<template>
  <div>
    <div class="search-panel">
      <el-col :span="9">
        <label>数据项名称：</label>
        <el-input v-model="itemname" placeholder="请输入内容" ></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="search">查询</el-button>
      </el-col>
    </div>
    <el-table
    :data="data"
    border
    style="width: 100%">
      <el-table-column
        label="网关名称"
        v-if="type == 'project'">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.devName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="数据项别名">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.alias }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="数据项名称">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.itemname }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="数据地址">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dataAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="类型">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.datatype }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="值">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.val }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="时间">
        <template scope="scope">
          <span style="margin-left: 10px">{{ scope.row.htime | time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="goToHistroyData(scope.row.devid, scope.row.itemid, scope.row.itemname)">历史数据</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import filtersMixin from '../mixins/filters'
import flattenDeep from 'lodash/flattenDeep'

export default {
  mixins: [filtersMixin],
  name: 'dataitemlist',
  props: ['deviceId', 'type'],
  data () {
    return {
      data: null,
      itemname: ''
    }
  },
  mounted: function () {
    this.requestData()
  },
  watch: {
    deviceId () {
      this.requestData()
    }
  },
  methods: {
    requestData () {
      if (!this.deviceId) {
        return
      }
      var devid = this.deviceId && this.deviceId.split(',')
      var reqs = devid.map((id) => {
        return this.$http.get('/data/current?device_id=' + id + '&dataitem_name=' + this.itemname)
      })
      Promise.all(reqs).then((allRes) => {
        this.data = flattenDeep(allRes.map((res) => {
          return res.data.currentDataList
        }))
      })
    },
    goToHistroyData (deviceId, itemid, itemname) {
      if (this.type === 'project') {
        this.$emit('show-history-data', {
          deviceId: deviceId,
          itemId: itemid,
          itemName: itemname
        })
      } else {
        this.$router.push({path: '/device/itemHistroyData/' + deviceId + '/' + itemid + '/' + itemname})
      }
    },
    search () {
      this.requestData()
    }
  }
}
</script>
<style lang="stylus" scoped>
.search-panel {
  border: 1px solid #ccc
  height: 100px
  line-height: 100px;
  margin: 20px;
  padding: 0 20px;
}
.el-col {
  display: inline-block;
}
.el-input {
  display: inline-block;
  width: 200px;
}
.el-table {
    padding: 20px;
}
</style>
